package com.cnswhy.cloud.server.comsumer.consumer.sms;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.cnswhy.cloud.common.core.mybatis.conditions.Wraps;
import com.cnswhy.cloud.common.rabbitmq.service.MessageService;
import com.cnswhy.cloud.common.redis.service.RedisService;
import com.cnswhy.cloud.server.common.entity.constant.AmqpExchange;
import com.cnswhy.cloud.server.common.entity.enumeration.SmsType;
import com.cnswhy.cloud.server.common.entity.mq.SmsSendMQ;
import com.cnswhy.cloud.server.comsumer.event.SmsQueryMessageEvent;
import com.cnswhy.cloud.server.comsumer.event.SmsSendMessageEvent;
import com.cnswhy.cloud.server.comsumer.service.SendSmsUtil;
import com.cnswhy.cloud.server.core.entity.SmsTempate;
import com.cnswhy.cloud.server.core.service.SmsTempateService;
import com.cnswhy.cloud.server.core.service.SysConfigService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component
@Slf4j
public class SmsMessageConsumer implements SmsSendMessageEvent, SmsQueryMessageEvent {
    @Autowired
    SysConfigService sysConfigService;
    @Autowired
    RedisService redisService;
    @Autowired
    SmsTempateService smsTempateService;
    @Autowired
    MessageService messageService;
    @Override
    public void send(SmsSendMQ smsSendVO) {
        log.info("发送短信:{}",JSON.toJSONString(smsSendVO));
        JSONObject jsonObject = new JSONObject();
        /*if("SMS_201451037".equals(smsSendVO.getTemplateCode())){

        }else if("SMS_200310059".equals(smsSendVO.getTemplateCode())){
            jsonObject.put("code", smsSendVO.getVerifyCode());
        }
        */
        SmsTempate smsTempate = smsTempateService.getOne(Wraps.<SmsTempate>lbQ().eq(SmsTempate::getType,smsSendVO.getSmsType()));
        if(SmsType.SMS_CODE.eq(smsTempate.getType())){
            jsonObject.put("code", smsSendVO.getVerifyCode());
        }else if (SmsType.SPELL_SUCCESS.eq(smsTempate.getType())){

        }
        try {
            String res = SendSmsUtil.sendSms(smsSendVO.getMobile(), smsTempate.getSignName(), smsTempate.getTemplateCode(), smsTempate.getAccessKeyId(), smsTempate.getAccessSecret(), JSON.toJSONString(jsonObject));
            JSONObject jsonObject1 = JSON.parseObject(res);
            if("OK".equals(jsonObject1.getString("Code"))){
                log.info("短信发送成功");
            }
            smsSendVO.setBizId(jsonObject1.getString("BizId"));
            smsSendVO.setQueryTimes(0L);
            messageService.sendAndQueryTimes(AmqpExchange.SEND_MESSAGE_QUERY,smsSendVO,smsSendVO.getQueryTimes());
            //{"Message":"OK","RequestId":"47A9C084-5CCF-405E-B7C9-F1ED36600F5B","BizId":"754624703955391728^0","Code":"OK"}
            /*

        boolean flag = false;
        if (StringUtils.isNotBlank(res) && "OK".equals(JSON.parseObject(res).getString("Code")))
        {
            flag = true;
        }
        return flag;*/
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    public void query(SmsSendMQ smsSendVO) {
        log.info("短信:{} 查询返回状态",JSON.toJSONString(smsSendVO));
        //TODO
        /*SmsTempate smsTempate = smsTempateService.getOne(Wraps.<SmsTempate>lbQ().eq(SmsTempate::getType,smsSendVO.getSmsType()));
        log.info("短信:{} 查询返回状态",JSON.toJSONString(smsSendVO));
        try {
            String res = SendSmsUtil.query(smsSendVO.getMobile(), smsSendVO.getBizId(), smsTempate.getAccessKeyId(), smsTempate.getAccessSecret());
        } catch (Exception e) {
            e.printStackTrace();
        }*/
    }
}
